package batch.makeFile;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.log4j.*;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.io.*;
import java.util.Date;

import batch.common.db.ConnectionManager;

public class makeUserFileForRus
{
    final String TODAY = DateFormatUtils.format(new Date(), "yyyyMMdd");

    public makeUserFileForRus() {
        ;
    }

    public static void main(String [] args) {
        Logger.getLogger("makefile.makeUserFileForRus").info("( * ) Start.");

        makeUserFileForRus mkRus = new makeUserFileForRus();
        mkRus.startMake();

        Logger.getLogger("makefile.makeUserFileForRus").info("( * ) End.");
    }

    //Make a log file and a data file.
    public void startMake(){
        Connection   conn = null;
        Statement    stmt = null;
        ResultSet    rs   = null;
        ResultSetMetaData  rsmd = null;
        OutputStreamWriter oswFile = null;

        String fileNm1  = "/home01/sfc/batch/makeFile/data/rus_member_"  + TODAY + ".csv";

        int cnt = 0;
        int rsCnt = 0;

        StringBuffer sbTemp = new StringBuffer();

        StringBuffer sQuery = new StringBuffer()
                             .append("SELECT ID                                                                                                                  \n")
                             .append("     , REPLACE(REPLACE(REPLACE(REPLACE(FIRSTNAME, CHR(10), ''), CHR(13), ''), 'WAP_TEMP', ''), 'SIM_TEMP', '') FIRSTNAME   \n")
                             .append("     , REPLACE(REPLACE(REPLACE(REPLACE(LASTNAME, CHR(10), ''), CHR(13), ''), 'WAP_TEMP', ''), 'SIM_TEMP', '') LASTNAME     \n")
                             .append("     , REPLACE(REPLACE(GENDER, 'W', ''), 'S', '') GENDER                                                                   \n")
                             .append("     , REPLACE(REPLACE(REPLACE(REPLACE(BIRTH, CHR(10), ''), CHR(13), ''), 'WAP_TEMP', ''), 'SIM_TEMP', '') BIRTH           \n")
                             .append("     , REPLACE(REPLACE(ADDRESS, CHR(10), ''), CHR(13), '') ADDRESS                                                         \n")
                             .append("     , REPLACE(REPLACE(POSTCODE, CHR(10), ''), CHR(13), '') POSTCODE                                                       \n")
                             .append("     , REPLACE(REPLACE(REPLACE(REPLACE(EMAIL, CHR(10), ''), CHR(13), ''), 'WAP_TEMP', ''), 'SIM_TEMP', '') EMAIL           \n")
                             .append("     , HP_BRAND                                                                                                            \n")
                             .append("     , REPLACE(REPLACE(HP_MODEL, 'WAP_TEMP', ''), 'SIM_TEMP', '') HP_MODEL                                                 \n")
                             .append("     , REPLACE(REPLACE(HP_IMEI, 'WAP_TEMP', ''), 'SIM_TEMP', '') HP_IMEI                                                   \n")
                             .append("     , REPLACE(REPLACE(HP_NUM, 'WAP_TEMP', ''), 'SIM_TEMP', '') HP_NUM                                                     \n")
                             .append("     , REPLACE(REPLACE(REPLACE(REPLACE(HP_PROVIDER, 'WAP_TEMP', ''), 'SIM_TEMP', ''), '000000', ''), ' ', '') HP_PROVIDER                                           \n")
                             .append("     , REPLACE(REPLACE(HP_BUY_YM, 'yyyymm', ''), ' ', '') HP_BUY_YM                                                        \n")
                             .append("     , REPLACE(REPLACE(HP_SN, 'WAP_TEMP', ''), 'SIM_TEMP', '') HP_SN                                                       \n")
                             .append("  FROM G_MB_MEMBER                                                                                                         \n")
                             .append(" WHERE REG_COUNTRY = 'rus'                                                                                                 \n")
                             .append("   AND DELETED IS NULL                                                                                                     \n")
                             .append("   AND CREATED BETWEEN TO_DATE('20060715000000', 'YYYYMMDDHH24MISS')                                                       \n")
                             .append("                   AND TO_DATE('20060916000000', 'YYYYMMDDHH24MISS')                                                       \n")
                             .append("   AND MEMBERGBN NOT LIKE '05314%'                                                                                         \n");



        try {
            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(true);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sQuery.toString());

            oswFile = new OutputStreamWriter(new FileOutputStream(fileNm1, false), "CP1251");

            sbTemp.append("\"ID\""         ).append(",")
                  .append("\"FIRST NAME\"" ).append(",")
                  .append("\"LAST NAME\""  ).append(",")
                  .append("\"GENDER\""     ).append(",")
                  .append("\"BIRTHDAY\""   ).append(",")
                  .append("\"ADDRESS\""    ).append(",")
                  .append("\"POSTCODE\""   ).append(",")
                  .append("\"EMAIL\""      ).append(",")
                  .append("\"HP_BRAND\""   ).append(",")
                  .append("\"HP_MODEL\""   ).append(",")
                  .append("\"HP_IMEI\""    ).append(",")
                  .append("\"HP_NUM\""     ).append(",")
                  .append("\"HP_PROVIDER\"").append(",")
                  .append("\"HP_BUY_YM\""  ).append(",")
                  .append("\"HP_SN\""      ).append("\n");

            oswFile.write(sbTemp.toString());

            while(rs.next()){
                rsmd = rs.getMetaData();
                rsCnt = rsmd.getColumnCount();

                sbTemp.setLength(0);

                for(int i=1;i<=rsCnt;i++) {
                    sbTemp.append("\"");

                    sbTemp.append(ObjectUtils.toString(rs.getObject(i), ""));

                    if( i == rsCnt ){
                        sbTemp.append("\"\n");
                    }
                    else{
                        sbTemp.append("\",");
                    }
                }

                oswFile.write(sbTemp.toString());
                cnt++;

                if(cnt % 500 == 0){
                    Logger.getLogger("makefile.makeUserFileForRus").info("[MEMBER]here we are == " + cnt);
                }
            }

            Logger.getLogger("makefile.makeUserFileForRus").info("[MEMBER]Total Count : " + cnt);

            if (rs   != null) try { rs.close(); }   catch (Exception e) { e.printStackTrace(); }
            if (oswFile != null) try { oswFile.close(); } catch (Exception e) { e.printStackTrace(); }

        }
        catch(Exception ex) {
            ex.printStackTrace();

            Logger.getLogger("makefile.makeUserFileForRus").info(ex.toString());
            StackTraceElement[] stElement = ex.getStackTrace();
            for(int p=0; p<stElement.length;p++){
                Logger.getLogger("makefile.makeUserFileForRus").info(stElement[p].toString());
            }
        }
        finally {
            if (rs   != null) try { rs.close(); }   catch (Exception e) { e.printStackTrace(); }
            if (stmt != null) try { stmt.close(); } catch (Exception e) { e.printStackTrace(); }
            if (conn != null) try { conn.close(); } catch (Exception e) { e.printStackTrace(); }
            if (oswFile != null) try { oswFile.close(); } catch (Exception e) { e.printStackTrace(); }
        }
    }
}